package com.xing.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.commonutils.JwtUtils;
import com.xing.commonutils.R;
import com.xing.order.entity.TOrder;
import com.xing.order.service.TOrderService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author xingfuao
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/order/t-order")
@CrossOrigin
@ApiModel("支付订单控制类")
public class TOrderController {

    @Autowired
    private TOrderService tOrderService;

    //根据课程id和用户id创建订单，返回订单id
    @ApiOperation("1.生成订单的方法")
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request) {
        String jwtToken = JwtUtils.getJwtToken("1080736474267144193", "用户XJtDfaYeKk");
        System.out.println("jwtToken=====>:" + jwtToken);

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //swagger取代不了的原因，就是没传token，导致memberId解析出来为空，所以导致最终的数据不能用的。
        String orderId = tOrderService.createOrders(courseId, memberId);
        return R.ok().data("orderId", orderId);
    }

    /**
     * 根据【用户id、课程id】查询订单表中的状态
     *
     * @param memberId
     * @param courseId
     * @return
     */
    @GetMapping("/isBuyCourse/{memberId}/{courseId}")
    public Boolean isBuyCourse(@PathVariable String memberId, @PathVariable String courseId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);

        int result = tOrderService.count(wrapper);

        if (result > 0) {  //已经支付
            return true;
        } else {
            return false;
        }

    }


}

